package org.BooCrack.bookstore.pay.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.BooCrack.bookstore.common.pojo.BSResult;
import org.BooCrack.bookstore.common.utils.BSResultUtil;
import org.BooCrack.bookstore.common.utils.Response;
import org.BooCrack.bookstore.common.utils.ResponseResult;
import org.BooCrack.bookstore.model.entity.OrderShipping;
import org.BooCrack.bookstore.model.entity.Orders;
import org.BooCrack.bookstore.model.entity.Store;
import org.BooCrack.bookstore.model.entity.User;
import org.BooCrack.bookstore.model.entity.custom.OrderCustom;
import org.BooCrack.bookstore.model.service.IOrderService;
import org.BooCrack.bookstore.model.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("admin/order")
@Api(value = "v1", tags = "8-1.后台管理系统订单模块接口")
@RequestMapping("admin/V3")
@RequiresPermissions("order-manage")
public class AdminOrderController {


    @Autowired
    private IOrderService orderService;

    @Autowired
    private IUserService userService;


    /**
     * 订单列表
     *
     * @param request
     * @return
     */
//    @RequestMapping("/list")
    @GetMapping("/order")
    @RequiresPermissions("order-list")
    @ApiOperation(value = "订单列表", notes = "订单列表")
    public ResponseResult<Map<String, Object>> orderList(HttpServletRequest request){

        Store loginStore = (Store) request.getSession().getAttribute("loginStore");

        List<OrderCustom> orderCustoms = orderService.findOrdersByStoreId(loginStore.getStoreId());

        request.setAttribute("orderCustoms", orderCustoms);

//        return "admin/order/list";
        Map<String, Object> map = new HashMap<>();
        map.put("orderCustoms", orderCustoms);
        map.put("Session", loginStore);
        return Response.createOkResp("findOrdersByStoreId",map);
    }

    /**
     * s
     * 查看订单详情
     *
     * @param orderId
     * @return
     */
//    @RequestMapping("/toUpdate/{orderId}")
    @GetMapping("/order/{orderId}")
    @RequiresPermissions("order-edit")
    @ApiOperation(value = "查看订单详情", notes = "查看订单详情")
    public ResponseResult<Map<String, Object>> updateOrder(@PathVariable("orderId") String orderId, Model model) {

        OrderCustom orderCustom = orderService.findOrderCustomById(orderId);

        User buyer = userService.findById(orderCustom.getOrder().getUserId());
//        model.addAttribute("orderCustom", orderCustom);
//        model.addAttribute("buyer", buyer);
//        return "admin/order/edit";
        Map<String, Object> map = new HashMap<>();
        map.put("orderCustom", orderCustom);
        map.put("buyer", buyer);
        return Response.createOkResp("findOrderCustomById",map);
    }

    @PostMapping("/order")
    @RequiresPermissions("order-edit")
    @ApiOperation(value = "订单修改", notes = "订单修改")
    public ResponseResult<Map<String, Object>> updateOrder(Orders order, OrderShipping orderShipping, Model model) {

        OrderCustom orderCustom = new OrderCustom();
        orderCustom.setOrder(order);
        orderCustom.setOrderShipping(orderShipping);
        orderService.updateOrder(orderCustom);
        model.addAttribute("saveMsg", "保存成功");
//        return "redirect:/admin/V3/order";

        Map<String, Object> map = new HashMap<>();
        map.put("saveMsg","保存成功");
        map.put("orderCustom", orderCustom);
        return Response.createOkResp("updateOrder",map);
    }

    @DeleteMapping("/order/{orderId}")
    @RequiresPermissions("order-delete")
    @ApiOperation(value = "订单删除", notes = "订单删除")
    public ResponseResult<Object> deletion(@PathVariable("orderId") String orderId) {

        BSResult bsResult = orderService.deleteOrder(orderId);

        if (bsResult.getCode() == 200) {
//            return "redirect:/admin/V3/order";
            Map<String, Object> map = new HashMap<>();
            map.put("orderId", orderId);
            return Response.createOkResp("deleteOrder",map);
        }
//        return "exception";
        return Response.createOkResp("exception");
    }

    /**
     * 发货
     *
     * @param orderId
     * @return
     */
    @RequiresPermissions("order-edit")
//    @RequestMapping("/post/{orderId}")
    @GetMapping("/order/post/{orderId}")
    @ApiOperation(value = "订单发货", notes = "订单发货")
    public ResponseResult<Object> postOrder(@PathVariable("orderId") String orderId){
        BSResult bsResult =  orderService.postOrder(orderId);
        if (bsResult.getCode() == 200) {
//            return "redirect:/admin/V3/order";
            Map<String, Object> map = new HashMap<>();
            map.put("orderId", orderId);
            return Response.createOkResp("postOrder",map);
        }
//        return "exception";
        return Response.createOkResp("exception");
    }

    /**
     * 查看买家信息
     * @param userId
     * @param model
     * @return
     */
//    @RequestMapping("/buyer/{userId}")
    @GetMapping("/order/buyer/{userId}")
    @ResponseBody
    @RequiresPermissions("order-list")
    @ApiOperation(value = "查看买家信息", notes = "查看买家信息")
    public BSResult buyerInfo(@PathVariable("userId") Integer userId,Model model){
        User buyer = userService.findById(userId);
        return BSResultUtil.success(buyer);
    }
}
